﻿using System;
using System.Configuration;

namespace YueRu.Common
{
    public class MailSender
    {
        string _smtpServer = ConfigurationManager.AppSettings["SmtpServer"];
        string _userName = ConfigurationManager.AppSettings["UserName"];
        string _pwd = ConfigurationManager.AppSettings["Pwd"];
        int _smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]);
        string _authorName = ConfigurationManager.AppSettings["AuthorName"];
        string _to = ConfigurationManager.AppSettings["To"];


        /// <summary>smtp服务器地址</summary>
        public string SmtpServer
        {
            get { return _smtpServer; }
            set { _smtpServer = value; }
        }

        /// <summary>发件人</summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        /// <summary>密码</summary>
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }

        /// <summary>端口号</summary>
        public int SmtpPort
        {
            get { return _smtpPort; }
            set { _smtpPort = value; }
        }

        /// <summary>显示名称</summary>
        public string AuthorName
        {
            get { return _authorName; }
            set { _authorName = value; }
        }

        /// <summary>收件人</summary>
        public string To
        {
            get { return _to; }
            set { _to = value; }
        }


        /// <summary>发送邮件</summary>
        /// <param name="subject">标题</param>
        /// <param name="body">邮件内容</param>
        public void Send(string subject, string body)
        {

            var toList = StringPlus.GetSubStringList(StringPlus.ToDbc(_to), ',');
            var smtp = new OpenSmtp.Mail.Smtp(_smtpServer, _userName, _pwd, _smtpPort);
            foreach (var s in toList)
            {
                var msg = new OpenSmtp.Mail.MailMessage
                {
                    From = new OpenSmtp.Mail.EmailAddress(_userName, _authorName)
                };

                msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
                msg.HtmlBody = body;  //设置邮件正文,并指定格式为 html 格式
                msg.Subject = subject; //设置邮件标题
                msg.Charset = "gb2312"; //指定邮件正文的编码
                smtp.SendMail(msg);  //发送邮件
            }
        }

        /// <summary>
        /// 发送邮件到指定人的邮箱
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="address">发送地址用","分割</param>
        public void Send(string subject, string body, string address)
        {
            var toList = StringPlus.GetSubStringList(StringPlus.ToDbc(address), ',');
            var smtp = new OpenSmtp.Mail.Smtp(_smtpServer, _userName, _pwd, _smtpPort);
            foreach (var s in toList)
            {
                var msg = new OpenSmtp.Mail.MailMessage
                {
                    From = new OpenSmtp.Mail.EmailAddress(_userName, _authorName)
                };

                msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
                msg.HtmlBody = body;  //设置邮件正文,并指定格式为 html 格式
                msg.Subject = subject; //设置邮件标题
                msg.Charset = "gb2312"; //指定邮件正文的编码
                smtp.SendMail(msg);  //发送邮件
            }
        }
    }
}
